package com.img;

import com.util.*;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import java.awt.image.ImageObserver;
import java.awt.Image;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Images extends JPanel {
	
	private String IMG_NAME="";
	private String IMG_URL="";
	private String IMG_PATH="c:/temp";
	private String IMG_EXT="";
	
	public static void main(String[] args) {
		try {
			Image image = null;
	    	Images clImg = new Images();
	    	String ImageUrl = "http://vndemo.0fees.net/images/banner/home-banner-3.png";
	    	//String ImageUrl = "http://www.mywickedspace.com/graphics/glitter/Animals/i53855315_83351.gif";
	    	//String ImageUrl="http://dantri4.vcmedia.vn/okcccccccccccccGqqSy/Image/2011/08/ok_79146.jpg";
	    	image = clImg.readImageFromUrl(ImageUrl);
	    	
	    	//System.out.println("Truonghm: "+ImageUrl.substring(ImageUrl.length()-3));
	    	
	    	int iWidth = clImg.getImageWidth(image);
	    	System.out.println("Width: "+iWidth);
	    	
	    	int iHeight = clImg.getImageHeoght(image);
	    	System.out.println("Height: "+iHeight);
	    	
	    	
	    	BufferedImage bImage = toBufferedImage(image);
	    	
	    	clImg.setImgUrl(ImageUrl);
	    	String fileName = clImg.saveImage();
	    	//System.out.println(fileName); 
	    		    	
		    // Use a label to display the image
		    JFrame frame = new JFrame();
		    JLabel label = new JLabel(new ImageIcon(image));
		    frame.getContentPane().add(label, BorderLayout.CENTER);
		    frame.pack();
		    frame.setVisible(true);
		    
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	
	public void setFileName(){
		RandomString rand =new RandomString();
		this.IMG_NAME= rand.get(20);
	}
	
	
	public String getFileName(){
		return this.IMG_NAME;
	}
	
	
	public void setImgUrl(String url){
		this.IMG_URL = url;
	}
	
	public String getImgUrl(){
		return this.IMG_URL;
	}
	
	
	public void setImgPath(String path){
		this.IMG_PATH = path;
	}
	
	public String getImgPath(){
		return this.IMG_PATH;
	}
	
	public void setImgExt(String imgUrl){
		this.IMG_EXT = imgUrl.substring(imgUrl.length()-3);
	}
	
	public String getImgExt(){
		return this.IMG_EXT;
	}
	
	
	public String saveImage(){
		try {
			System.out.println(this.getImgUrl());
			Image image=readImageFromUrl(this.getImgUrl());
			BufferedImage bImage = toBufferedImage(image);
			
			this.setFileName();
			this.setImgExt(this.getImgUrl());
			
	    	save(bImage); 
			
			return this.getFileName()+"."+this.getImgExt();
			
		} catch (Exception e) {
			return "FALSE";
		}
	}
	
	
	private String save(BufferedImage image) {
		
		String diskPath = this.getImgPath() + "/"+ this.getFileName() + "." + this.getImgExt();
        File file = new File(diskPath);        
        try {
            ImageIO.write(image, this.getImgExt(), file);  // ignore returned boolean
            return "TRUE";
        } catch(IOException e) {
            System.out.println("Write error for " + file.getPath() + ": " + e.getMessage());
            return "FALSE";
        }
    }
	
	private static BufferedImage toBufferedImage(Image src) {
        int w = src.getWidth(null);
        int h = src.getHeight(null);
        int type = BufferedImage.TYPE_INT_RGB;  // other options
        BufferedImage dest = new BufferedImage(w, h, type);
        Graphics2D g2 = dest.createGraphics();
        g2.drawImage(src, 0, 0, null);
        g2.dispose();
        return dest;
    }
	
	  /**
	   * get width image
	   * @param Image image
	   * @return
	   */
	  public int getImageWidth(Image image){
		  int iWidth=image.getWidth(this);		  
		  return iWidth;
	  }
	  
	  
	  /**
	   * get width image
	   * @param Image image
	   * @return
	   */
	  public int getImageHeoght(Image image){
		  int iHeight=image.getHeight(this);		  
		  return iHeight;
	  }
	
	/**
	 * Read from a URL
	 * @param p_url=url image VD: (http://vndemo.0fees.net/images/banner/home-banner-3.png)
	 * @return Image
	 */
	
	public Image readImageFromUrl(String p_url){
		try {
			
			Image image=null;
			URL url = new URL(p_url);
	        image = ImageIO.read(url);	        
			return image;
			
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	// Read from a file
	public Image readImageFromFile(String p_file){
		try {
			
			Image image=null;
			File sourceimage = new File(p_file);
	        image = ImageIO.read(sourceimage);	        
			return image;
			
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	// Read from an input stream
	public Image readImageFromStream(String p_file){
		try {
			
			Image image=null;
			InputStream is = new BufferedInputStream(new FileInputStream(p_file));
		    image = ImageIO.read(is);
		    
			return image;
			
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
}
